﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using LoadNorthwindDB;
using System.Data.Objects.DataClasses;
using System.Data.SqlClient;
using System.Transactions;

namespace Ex9.NewOrder
{
    public class CreateNewOrder
    {
        public class UnfinishedOrderDetail
        {
            public int ProductID { get; set; }
            public decimal Price { get; set; }
            public byte Quantity { get; set; }
            public float Discount { get; set; }

            public UnfinishedOrderDetail(int productId, decimal price, byte quantity, float disc)
            {
                this.ProductID = productId;
                this.Price = price;
                this.Quantity = quantity;
                this.Discount = disc;
            }
        }

        public static void Main()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                NorthwindEntities ent = new NorthwindEntities();
                using (ent)
                {
                    Customer myCustomer = ent.Customers.FirstOrDefault(c => c.CustomerID == "99999");
                    Employee myEmployee = ent.Employees.FirstOrDefault(m => m.EmployeeID == 7);
                    Shipper myShipper = ent.Shippers.FirstOrDefault(s => s.ShipperID == 1);
                    EntityCollection<UnfinishedOrderDetail> orderDetails = new EntityCollection<UnfinishedOrderDetail>();
                    UnfinishedOrderDetail orderDetail = new UnfinishedOrderDetail(5, 13M, 10, 1f);
                    UnfinishedOrderDetail orderDetail2 = new UnfinishedOrderDetail(6, 13M, 10, 1f);
                    UnfinishedOrderDetail orderDetail3 = new UnfinishedOrderDetail(1, 13M, 10, 0f);
                    orderDetails.Add(orderDetail);
                    orderDetails.Add(orderDetail2);
                    orderDetails.Add(orderDetail3);
                    AddNewOrderNubieWay(myCustomer, myEmployee, myShipper, orderDetails,
                        "Aliaska", "Montana", "Alekovec", "Montana", "1654", "Bulgaria");
                }
            }
        }

        public static Order_Detail CreateOrderDetail(int orderID, UnfinishedOrderDetail detail)
        {
            Order_Detail newOrderDetail = new Order_Detail
            {
                OrderID = orderID,
                ProductID = detail.ProductID,
                UnitPrice = detail.Price,
                Quantity = detail.Quantity,
                Discount = detail.Discount
            };
            return newOrderDetail;
        }


        public static void AddNewOrderNubieWay(Customer customer, Employee employee, Shipper shipper,
                EntityCollection<UnfinishedOrderDetail> unfinishedOrderDetails, string shipName,
                string shipAddress, string shipCity, string shipRegion, string shipPostalCode, 
                string shipCountry, DateTime? orderDate = null, DateTime? requiredDate = null, 
                DateTime? shippedDate = null, int? shipVia = null, decimal? freight = null)
        {
            NorthwindEntities contex = new NorthwindEntities();
            
            using (contex)
            {
                Order order = new Order();

                order.CustomerID = customer.CustomerID;
                order.EmployeeID = employee.EmployeeID;
                order.OrderDate = orderDate;
                order.RequiredDate = requiredDate;
                order.ShippedDate = shippedDate;
                order.ShipVia = shipVia;
                order.Freight = freight;
                order.ShipName = shipName;
                order.ShipAddress = shipAddress;
                order.ShipCity = shipCity;
                order.ShipRegion = shipRegion;
                order.ShipPostalCode = shipPostalCode;
                order.ShipCountry = shipCountry;

                contex.Orders.AddObject(order);
                contex.SaveChanges();

                Order orderFromDB = contex.Orders.FirstOrDefault(o=>(o.CustomerID == customer.CustomerID) && ( 
                    o.EmployeeID == employee.EmployeeID) && (o.ShipAddress == shipAddress));

                int orderID = orderFromDB.OrderID;
                foreach (UnfinishedOrderDetail detail in unfinishedOrderDetails)
                {
                    Order_Detail newOrderDetail = CreateOrderDetail(orderID, detail);
                    orderFromDB.Order_Details.Add(newOrderDetail);
                }
                
                contex.SaveChanges();
            }
        }

    }
}
